package com.example.mall.web.controller;

import com.example.mall.domain.ErrorLogs;
import com.example.mall.dto.ErrorLogsDto;
import com.example.mall.exception.ErrorEnumCode;
import com.example.mall.exception.GlobalCustomerException;
import com.example.mall.query.ErrorLogsQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.result.JSONResult;
import com.example.mall.service.IErrorLogsService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/errorLogs")
public class ErrorLogsController {

    @Autowired
    public IErrorLogsService errorLogsService;

    /**
     * 分页查询错误日志表信息
     * @param errorLogsQuery
     * @return
     */
    @GetMapping("queryPage")
    @ApiOperation(value = "分页查询错误日志表信息",notes = "分页查询错误日志表信息",httpMethod = "GET")
    public JSONResult queryPage(ErrorLogsQuery errorLogsQuery){
        PageDTO<ErrorLogsDto> errorLogsDtoPageDTO = errorLogsService.queryPage(errorLogsQuery);
        return JSONResult.success("查询成功",errorLogsDtoPageDTO);
    }

    /**
     * 根据id查询错误日志表信息
     * @param errorId
     * @return
     */
    @GetMapping("query/{errorId}")
    @ApiOperation(value = "根据id查询错误日志表信息",notes = "根据id查询错误日志表信息",httpMethod = "GET")
    public JSONResult query(@PathVariable("errorId")Long errorId){
        //1.根据id查询用户信息
        if (errorId == null){
            throw new GlobalCustomerException(ErrorEnumCode.ERRORID_NOT_ID);
        }
        ErrorLogs errorLogs = errorLogsService.getById(errorId);
        return JSONResult.success("查询成功",errorLogs);
    }

    /**
     * 根据id查询错误日志表信息
     * @param errorId
     * @return
     */
    @GetMapping("queryErrorLogsById/{errorId}")
    @ApiOperation(value = "根据id查询错误日志表信息",notes = "根据id查询错误日志表信息",httpMethod = "GET")
    public JSONResult queryErrorLogsById(@PathVariable("errorId")Long errorId){
        //1.根据id查询用户信息
        if (errorId == null){
            throw new GlobalCustomerException(ErrorEnumCode.ERRORID_NOT_ID);
        }
        ErrorLogs errorLogs = errorLogsService.queryErrorLogsById(errorId);
        return JSONResult.success("查询成功",errorLogs);
    }

    /**
     * 查询全部错误日志表信息
     * @param
     * @return
     */
    @GetMapping("queryErrorLogsList")
    @ApiOperation(value = "查询全部错误日志表信息",notes = "查询全部错误日志表信息",httpMethod = "GET")
    public JSONResult queryErrorLogsList(){
        List<ErrorLogs> errorLogs = errorLogsService.queryErrorLogsList();
        return JSONResult.success("查询成功",errorLogs);
    }

    /**
     * 新增错误日志表
     * @param errorLogsDto
     * @return
     */
    @PostMapping("insert")
    @ApiOperation(value = "新增错误日志表",notes = "新增错误日志表",httpMethod = "POST")
    public JSONResult insert(@RequestBody ErrorLogsDto errorLogsDto){
        errorLogsService.insert(errorLogsDto);
        return JSONResult.success("保存成功");
    }

    /**
     * 修改错误日志表信息
     * @param errorLogsDto
     * @return
     */
    @PostMapping("update")
    @ApiOperation(value = "修改错误日志表信息",notes = "修改错误日志表信息",httpMethod = "POST")
    public JSONResult update(@RequestBody ErrorLogsDto errorLogsDto){
        errorLogsService.update(errorLogsDto);
        return JSONResult.success("修改成功");
    }

    /**
     * 删除错误日志表信息
     * @param errorId
     * @return
     */
    @PostMapping("delete/{id}")
    @ApiOperation(value = "删除错误日志表信息",notes = "删除错误日志表信息",httpMethod = "POST")
    public JSONResult delete(@PathVariable("errorId") Long errorId) {
        errorLogsService.delete(errorId);
        return JSONResult.success("删除成功");
    }

    /**
     * 批量删除错误日志表信息
     * @param errorIds
     * @return
     */
    @PostMapping("deleteBatch")
    @ApiOperation(value = "批量删除错误日志表信息",notes = "批量删除错误日志表信息",httpMethod = "POST")
    public JSONResult deleteAll(@RequestBody List<Long> errorIds) {
        errorLogsService.deleteBatch(errorIds);
        return JSONResult.success("批量删除成功");
    }
}
